#include "OffFile.h"


OffFile::OffFile(const char *s)
{
    _faces = 0;
    _vertices = 0;
    load(s);
}
	
/*
 * Load in a float array the .off file. 
 */
void OffFile::load(const char *s)
{
    if (_vertices) 
	{
	    delete[] _vertices;
	    _vertices = 0;
	}
    if(_faces)
	{
	    delete [] _faces;
	    _faces = 0;
	}
    std::ifstream file(s, std::ios::in);
		
    if (!file)
	{
	    throw "Can't open file";
	}
		
    std::string key;
    file >> key;
    if(key != "OFF")
	{
	    file.close();
	    throw ("Not a .off file");
	}
		
    int nbVertices, nbFaces, nbEdges, fSize, trash, count = 0;
    file >> nbVertices >> nbFaces >> nbEdges;
    _sizeV = nbVertices*3;
    _vertices = new float[_sizeV];		
    for (int i = 0; i < _sizeV; i++)
	{
	    file >> _vertices[i];
	}
		
    file >> fSize;
    _nbfaces = nbFaces * fSize;
    int taille = _nbfaces* fSize;
    _faces = new GLuint[taille];
    for(int i = 0; i < nbFaces; i++)
	{
	    for(int j = 0; j < fSize; j++)
		file >> _faces[fSize*i+j];
	    file >> trash;
	}
    file.close();
}
	
float * OffFile::getVertices()
{
    return _vertices;
}
			
GLuint * OffFile::getFaces()
{
    return _faces;
}
	
long long int OffFile::getNbVertices()
{
    return _sizeV;
}
       
long long int OffFile::getNbFaces()
{
    return _nbfaces;
}
	
OffFile::~OffFile()
{
    if(_vertices) delete [] _vertices;
    if(_faces) delete [] _faces;
}

